package com.cqjudicial.umtest;

import android.content.Intent;
import android.os.Bundle;
import android.os.PersistableBundle;
import android.util.Log;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.cqjudicial.umtest.umeng.onkeyloginconfig.AuthPageConfig;
import com.cqjudicial.umtest.umeng.onkeyloginconfig.BaseUIConfig;
import com.umeng.umverify.UMResultCode;
import com.umeng.umverify.UMVerifyHelper;
import com.umeng.umverify.listener.UMTokenResultListener;
import com.umeng.umverify.model.UMTokenRet;

public class OnkeyLoginActivity extends AppCompatActivity {
    private static final String TAG = OnkeyLoginActivity.class.getSimpleName();

    private UMVerifyHelper mPhoneNumberAuthHelper;
    private UMTokenResultListener mTokenResultListener;
    private AuthPageConfig mUIConfig;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState, @Nullable PersistableBundle persistentState) {
        super.onCreate(savedInstanceState, persistentState);
        setContentView(R.layout.activity_main);
        sdkInit();
        mUIConfig = BaseUIConfig.init(this, mPhoneNumberAuthHelper);
        oneKeyLogin();
    }


    public void sdkInit() {
        mTokenResultListener = new UMTokenResultListener() {
            @Override
            public void onTokenSuccess(String s) {
                UMTokenRet tokenRet = null;
                try {
                    tokenRet = UMTokenRet.fromJson(s);
                    if (UMResultCode.CODE_START_AUTHPAGE_SUCCESS.equals(tokenRet.getCode())) {
                        Log.i("TAG1", "唤起授权页成功：" + s);
                    }

                    if (UMResultCode.CODE_GET_TOKEN_SUCCESS.equals(tokenRet.getCode())) {
                        Log.i("TAG2", "获取token成功：" + s);
                        mUIConfig.release();

                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onTokenFailed(String s) {
                Log.e(TAG, "获取token失败：" + s);
                UMTokenRet tokenRet = null;
                mPhoneNumberAuthHelper.quitLoginPage();
                try {
                    tokenRet = UMTokenRet.fromJson(s);
                    if (UMResultCode.CODE_ERROR_USER_CANCEL.equals(tokenRet.getCode())) {
                        //模拟的是必须登录 否则直接退出app的场景
//                        finish();
                    } else {

                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
                mUIConfig.release();
            }
        };
        mPhoneNumberAuthHelper = UMVerifyHelper.getInstance(this, mTokenResultListener);
        mPhoneNumberAuthHelper.setAuthSDKInfo("8Ihtq4hXDTKs5vmt3h1K4yBbfUKirqUPH6CvhAQJLTtpqnjIgnfKELrbw+SyLc98kHPKrBfMS7HwYkWLo9PEqBaSzDwlJiPZZGVSp+Vat1XaZwuUGtOI+0u65WBN9FjxRwRaRoU0R1MOZ3c0E7pQS/1gerw9YiRDC46Rk1whPQ9pr6wlnxz5qHU2mRLKaRBErbdC0DBA/2/aJh/IRhpswvzr23VNkwhlFMXfvie67ZrzI3h/OEIUP9u6J4pyjM+ZKbPWXSwRHz8NcK4jBfdfRIpBfB4yFBaSk7Ws9oNrJt2OxXjuXbzZ8g==");
    }

    /**
     * 进入app就需要登录的场景使用
     */
    private void oneKeyLogin() {
        mPhoneNumberAuthHelper = UMVerifyHelper.getInstance(getApplicationContext(), mTokenResultListener);
        mUIConfig.configAuthPage();
        getLoginToken(5000);
    }

    /**
     * 拉起授权页
     *
     * @param timeout 超时时间
     */
    public void getLoginToken(int timeout) {
        mPhoneNumberAuthHelper.getLoginToken(this, timeout);
    }


    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 1002) {
            if (resultCode == 1) {
//                mTvResult.setText("登陆成功：" + data.getStringExtra("result"));
            } else {
                //模拟的是必须登录 否则直接退出app的场景
                finish();
            }
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
//        mUIConfig.onResume();
    }

}
